home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / m68k / cc68k.arc / DECL.C < prev    next >
C/C++ Source or Header  |  1986-10-26  |  17KB  |  488 lines

  1. #include        "stdio.h"
  2. #include        "string.h"
  3. #include        "c.h"
  4. #include        "expr.h"
  5. #include        "gen.h"
  6. #include        "cglbdec.h"
  7.  
  8. /*
  9.  *    68000 C compiler
  10.  *
  11.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  12.  *  all commercial rights reserved.
  13.  *
  14.  *    This compiler is intended as an instructive tool for personal use. Any
  15.  *    use for profit without the written consent of the author is prohibited.
  16.  *
  17.  *    This compiler may be distributed freely for non-commercial use as long
  18.  *    as this notice stays intact. Please forward any enhancements or question
  19. s
  20.  *    to:
  21.  *
  22.  *        Matthew Brandt
  23.  *        Box 920337
  24.  *        Norcross, Ga 30092
  25.  */
  26.  
  27. TYP             *head = 0;
  28. TYP             *tail = 0;
  29. char            *declid = 0;
  30. TABLE           tagtable = {0,0};
  31. TYP             stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
  32.  
  33. int     imax(i,j)
  34. int     i,j;
  35. {       return (i > j) ? i : j;
  36. }
  37.  
  38. char    *litlate(s)
  39. char    *s;
  40. {       char    *p;
  41.         p =(char *)xalloc(strlen(s) + 1);
  42.         strcpy(p,s);
  43.         return p;
  44. }
  45.  
  46. TYP     *maketype(bt,siz)
  47. int     bt, siz;
  48. {       TYP     *tp;
  49.         tp =(TYP *)xalloc(sizeof(TYP));
  50.         tp->val_flag = 0;
  51.         tp->size =(long)siz;
  52.         tp->type = bt;
  53.         tp->sname = 0;
  54.         tp->lst.head = 0;
  55.         return tp;
  56. }
  57.  
  58.      decl(table)
  59. TABLE   *table;
  60. {       switch (lastst) {
  61.                 case kw_char:
  62.                         head = tail = maketype(bt_char,1);
  63.                         getsym();
  64.                         break;
  65.                 case kw_short:
  66.                         head = tail = maketype(bt_short,2);
  67.                         getsym();
  68.                         break;
  69.                 case kw_int: case kw_long:
  70.                         head = tail = maketype(bt_long,4);
  71.                         getsym();
  72.                         break;
  73.                 case kw_unsigned:
  74.                         head = tail = maketype(bt_unsigned,4);
  75.                         getsym();
  76.                         if( lastst == kw_int )
  77.                                 getsym();
  78.                         break;
  79.                 case id:                /* no type declarator */
  80.                         head = tail = maketype(bt_long,4);
  81.                         break;
  82.                 case kw_float:
  83.                         head = tail = maketype(bt_float,4);
  84.                         getsym();
  85.                         break;
  86.                 case kw_double:
  87.                         head = tail = maketype(bt_double,8);
  88.                         getsym();
  89.                         break;
  90.                 case kw_enum:
  91.                         getsym();
  92.                         declenum(table);
  93.                         break;
  94.                 case kw_struct:
  95.                         getsym();
  96.                         declstruct(bt_struct);
  97.                         break;
  98.                 case kw_union:
  99.                         getsym();
  100.                         declstruct(bt_union);
  101.                         break;
  102.                 }
  103. }
  104.  
  105.  decl1()
  106. {       TYP     *temp1, *temp2, *temp3, *temp4;
  107.         switch (lastst) {
  108.                 case id:
  109.                         declid = litlate(lastid);
  110.                         getsym();
  111.                         decl2();
  112.                         break;
  113.                 case star:
  114.                         temp1 = maketype(bt_pointer,4);
  115.                         temp1->btp = head;
  116.                         head = temp1;
  117.                         if(tail == NULL)
  118.                                 tail = head;
  119.                         getsym();
  120.                         decl1();
  121.                         break;
  122.                 case openpa:
  123.                         getsym();
  124.                         temp1 = head;
  125.                         temp2 = tail;
  126.                         head = tail = NULL;
  127.                         decl1();
  128.                         needpunc(closepa);
  129.                         temp3 = head;
  130.                         temp4 = tail;
  131.                         head = temp1;
  132.                         tail = temp2;
  133.                         decl2();
  134.                         temp4->btp = head;
  135.                         if(temp4->type == bt_pointer &&
  136.                                 temp4->val_flag != 0 && head != NULL)
  137.                                 temp4->size *= head->size;
  138.                         head = temp3;
  139.                         break;
  140.                 default:
  141.                         decl2();
  142.                         break;
  143.                 }
  144. }
  145.  
  146.  decl2()
  147. {       TYP     *temp1;
  148.         switch (lastst) {
  149.                 case openbr:
  150.                         getsym();
  151.                         temp1 = maketype(bt_pointer,0);
  152.                         temp1->val_flag = 1;
  153.                         temp1->btp = head;
  154.                         if(lastst == closebr) {
  155.                                 temp1->size = 0;
  156.                                 getsym();
  157.                                 }
  158.                         else if(head != NULL) {
  159.                                 temp1->size = intexpr() * head->size;
  160.                                 needpunc(closebr);
  161.                                 }
  162.                         else {
  163.                                 temp1->size =(long)intexpr();
  164.                                 needpunc(closebr);
  165.                                 }
  166.                         head = temp1;
  167.                         if( tail == NULL)
  168.                                 tail = head;
  169.                         decl2();
  170.                         break;
  171.                 case openpa:
  172.                         getsym();
  173.                         temp1 = maketype(bt_func,0);
  174.                         temp1->val_flag = 1;
  175.                         temp1->btp = head;
  176.                         head = temp1;
  177.                         if( lastst == closepa) {
  178.                                 getsym();
  179.                                 if(lastst == begin)
  180.                                         temp1->type = bt_ifunc;
  181.                                 }
  182.                         else
  183.                                 temp1->type = bt_ifunc;
  184.                         break;
  185.                 }
  186. }
  187.  
  188. int     alignment(tp)
  189. TYP     *tp;
  190. {       switch(tp->type) {
  191.                 case bt_char:           return AL_CHAR;
  192.                 case bt_short:          return AL_SHORT;
  193.                 case bt_long:           return AL_LONG;
  194.                 case bt_enum:           return AL_SHORT;
  195.                 case bt_pointer:
  196.                         if(tp->val_flag)
  197.                                 return alignment(tp->btp);
  198.                         else
  199.                                 return AL_POINTER;
  200.                 case bt_float:          return AL_FLOAT;
  201.                 case bt_double:         return AL_DOUBLE;
  202.                 case bt_struct:
  203.                 case bt_union:          return AL_STRUCT;
  204.                 default:                return AL_CHAR;
  205.                 }
  206. }
  207.  
  208. int     declare(table,al,ilc,ztype)
  209. /*
  210.  *      process declarations of the form:
  211.  *
  212.  *              <type>  <decl>, <decl>...;
  213.  *
  214.  *      leaves the declarations in the symbol table pointed to by
  215.  *      table and returns the number of bytes declared. al is the
  216.  *      allocation type to assign, ilc is the initial location
  217.  *      counter. if al is sc_member then no initialization will
  218.  *      be processed. ztype should be bt_struct for normal and in
  219.  *      structure declarations and sc_union for in union declarations.
  220.  */
  221. TABLE           *table;
  222. int               al;
  223. int             ilc;
  224. int               ztype;
  225. {       SYM     *sp, *sp1;
  226.         TYP     *dhead;
  227.         int     nbytes;
  228.         nbytes = 0;
  229.         decl(table);
  230.         dhead = head;
  231.         for(;;) {
  232.                 declid = 0;
  233.                 decl1();
  234.                 if( declid != 0) {      /* otherwise just struct tag... */
  235.                         sp =(SYM *)xalloc(sizeof(SYM));
  236.                         sp->name = declid;
  237.                         sp->storage_class = al;
  238.                         while( (ilc + nbytes) % alignment(head)) {
  239.                                 if( al != sc_member &&
  240.                                         al != sc_external &&
  241.                                         al != sc_auto) {
  242.                                         dseg();
  243.                                         genbyte(0);
  244.                                         }
  245.                                 ++nbytes;
  246.                                 }
  247.                         if( al == sc_static)
  248.                                 sp->value.i =(long) nextlabel++;
  249.                         else if( ztype == bt_union)
  250.                                 sp->value.i =(long) ilc;
  251.                         else if( al != sc_auto )
  252.                                 sp->value.i =(long)( ilc + nbytes);
  253.                         else
  254.                                 sp->value.i = -(ilc + nbytes + head->size);
  255.                         sp->tp = head;
  256.                         if( sp->tp->type == bt_func &&
  257.                                 sp->storage_class == sc_global )
  258.                                 sp->storage_class = sc_external;
  259.                         if(ztype == bt_union)
  260.                                 nbytes = imax(nbytes,(int)(sp->tp->size));
  261.                         else if(al != sc_external)
  262.                                 nbytes +=(int)sp->tp->size;
  263.                         if( sp->tp->type == bt_ifunc &&
  264.                                 (sp1 =(SYM *)search(sp->name,table->head)) != 0 &&
  265.                                 sp1->tp->type == bt_func )
  266.                                 {
  267.                                 sp1->tp = sp->tp;
  268.                                 sp1->storage_class = sp->storage_class;
  269.                                 sp1->value.i = sp->value.i;
  270.                                 sp = sp1;
  271.                                 }
  272.                         else
  273.                                 insert(sp,table);
  274.                         if( sp->tp->type == bt_ifunc) { /* function body follows
  275.  */
  276.                                 funcbody(sp);
  277.                                 return nbytes;
  278.                                 }
  279.                         if( (al == sc_global || al == sc_static) &&
  280.                                 sp->tp->type != bt_func)
  281.                                 doinit(sp);
  282.                         }
  283.                 if(lastst == semicolon)
  284.                         break;
  285.                 needpunc(comma);
  286.                 if(declbegin(lastst) == 0)
  287.                         break;
  288.                 head = dhead;
  289.                 }
  290.         getsym();
  291.         return nbytes;
  292. }
  293.  
  294. int     declbegin(st)
  295. int               st;
  296. {       return st == star || st == id || st == openpa ||
  297.                 st == openbr;
  298. }
  299.  
  300.  declenum(table)
  301. TABLE   *table;
  302. {       SYM     *sp;
  303.         TYP     *tp;
  304.         if( lastst == id) {
  305.                 if((sp = (SYM *)search(lastid,tagtable.head)) == 0) {
  306.                         sp = (SYM *)xalloc(sizeof(SYM));
  307.                         sp->tp =(TYP *)xalloc(sizeof(TYP));
  308.                         sp->tp->type = bt_enum;
  309.                         sp->tp->size = 2;
  310.                         sp->tp->lst.head = sp->tp->btp = 0;
  311.                         sp->storage_class = sc_type;
  312.                         sp->name = litlate(lastid);
  313.                         sp->tp->sname = sp->name;
  314.                         getsym();
  315.                         if( lastst != begin)
  316.                                 error(ERR_INCOMPLETE);
  317.                         else    {
  318.                                 insert(sp,&tagtable);
  319.                                 getsym();
  320.                                 enumbody(table);
  321.                                 }
  322.                         }
  323.                 else
  324.                         getsym();
  325.                 head = sp->tp;
  326.                 }
  327.         else    {
  328.                 tp =(TYP *)xalloc(sizeof(tp));
  329.                 tp->type = bt_short;
  330.                 if( lastst != begin)
  331.                         error(ERR_INCOMPLETE);
  332.                 else    {
  333.                         getsym();
  334.                         enumbody(table);
  335.                         }
  336.                 head = tp;
  337.                 }
  338. }
  339.  
  340.  enumbody(table)
  341. TABLE   *table;
  342. {       int     evalue;
  343.         SYM     *sp;
  344.         evalue = 0;
  345.         while(lastst == id) {
  346.                 sp = (SYM *)xalloc(sizeof(SYM));
  347.                 sp->value.i =(long)evalue++;
  348.                 sp->name = litlate(lastid);
  349.                 sp->storage_class = sc_const;
  350.                 sp->tp = &stdconst;
  351.                 insert(sp,table);
  352.                 getsym();
  353.                 if( lastst == comma)
  354.                         getsym();
  355.                 else if(lastst != end)
  356.                         break;
  357.                 }
  358.         needpunc(end);
  359. }
  360.  
  361.  declstruct(ztype)
  362. /*
  363.  *      declare a structure or union type. ztype should be either
  364.  *      bt_struct or bt_union.
  365.  */
  366. int               ztype;
  367. {       SYM     *sp;
  368.         TYP     *tp;
  369.         if(lastst == id) {
  370.                 if((sp = (SYM *)search(lastid,tagtable.head)) == 0) {
  371.                         sp = (SYM *)xalloc(sizeof(SYM));
  372.                         sp->name = litlate(lastid);
  373.                         sp->tp =(TYP *)xalloc(sizeof(TYP));
  374.                         sp->tp->type = ztype;
  375.                         sp->tp->lst.head = 0;
  376.                         sp->storage_class = sc_type;
  377.                         sp->tp->sname = sp->name;
  378.                         getsym();
  379.                         if(lastst != begin)
  380.                                 error(ERR_INCOMPLETE);
  381.                         else    {
  382.                                 insert(sp,&tagtable);
  383.                                 getsym();
  384.                                 structbody(sp->tp,ztype);
  385.                                 }
  386.                         }
  387.                 else
  388.                         getsym();
  389.                 head = sp->tp;
  390.                 }
  391.         else    {
  392.                 tp = (TYP *)xalloc(sizeof(TYP));
  393.                 tp->type = ztype;
  394.                 tp->sname = 0;
  395.                 tp->lst.head = 0;
  396.                 if( lastst != begin)
  397.                         error(ERR_INCOMPLETE);
  398.                 else    {
  399.                         getsym();
  400.                         structbody(tp,ztype);
  401.                         }
  402.                 head = tp;
  403.                 }
  404. }
  405.  
  406.  structbody(tp,ztype)
  407. TYP             *tp;
  408. int               ztype;
  409. {       int     slc;
  410.         slc = 0;
  411.         tp->val_flag = 1;
  412.         while( lastst != end) {
  413.                 if(ztype == bt_struct)
  414.                         slc += declare(&(tp->lst),sc_member,slc,ztype);
  415.                 else
  416.                         slc = imax(slc,declare(&tp->lst,sc_member,0,ztype));
  417.                 }
  418.         tp->size =(long)slc;
  419.         getsym();
  420. }
  421.  
  422.  compile()
  423. /*
  424.  *      main compiler routine. this routine parses all of the
  425.  *      declarations using declare which will call funcbody as
  426.  *      functions are encountered.
  427.  */
  428. {       while(lastst != eof) {
  429.                 dodecl(sc_global);
  430.                 if( lastst != eof)
  431.                         getsym();
  432.                 }
  433.         dumplits();
  434. }
  435.  
  436.  dodecl(defclass)
  437. int               defclass;
  438. {
  439.         for(;;) {
  440.             switch(lastst) {
  441.                 case kw_register:
  442.                         getsym();
  443.                         if( defclass != sc_auto && defclass != sc_member )
  444.                                 error(ERR_ILLCLASS);
  445.                         goto do_decl;
  446.                 case id:
  447.                         if(defclass == sc_auto)
  448.                                 return;
  449. /*                      else fall through to declare    */
  450.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  451.                 case kw_long: case kw_struct: case kw_union:
  452.                 case kw_enum: case kw_void:
  453.                 case kw_float: case kw_double:
  454. do_decl:            if( defclass == sc_global)
  455.                         lc_static +=
  456.                             declare(&gsyms,sc_global,lc_static,bt_struct);
  457.                     else if( defclass == sc_auto)
  458.                         lc_auto +=
  459.                             declare(&lsyms,sc_auto,lc_auto,bt_struct);
  460.                     else
  461.                         declare(&lsyms,sc_auto,0,bt_struct);
  462.                     break;
  463.                 case kw_static:
  464.                         getsym();
  465.                         if( defclass == sc_member)
  466.                             error(ERR_ILLCLASS);
  467.                         if( defclass == sc_auto )
  468.                             lc_static +=
  469.                                 declare(&lsyms,sc_static,lc_static,bt_struct);
  470.                         else
  471.                             lc_static +=
  472.                                 declare(&gsyms,sc_static,lc_static,bt_struct);
  473.                         break;
  474.                 case kw_extern:
  475.                         getsym();
  476.                         if( defclass == sc_member)
  477.                             error(ERR_ILLCLASS);
  478.                         ++global_flag;
  479.                         declare(&gsyms,sc_external,0,bt_struct);
  480.                         --global_flag;
  481.                         break;
  482.                 default:
  483.                         return;
  484.                 }
  485.             }
  486. }
  487.  
  488.